using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

[Serializable]
public class ShiJian
{

    public int id;
    public string neirong;
    public string ewaineirong;
    public List<int> jiacheng;
    public List<int> nextIn;
    public List<int> nextEx;
    public List<int> lastIn;
    public List<int> lastEx;
    public int last;
    public bool cycle;
    public int xiyou;
    public string shuxin;
    public List<shuzhijiance> shuzhijiances;
    public ShiJian()
    {
       
        last = 0;
    }
}
[Serializable]
public class shuzhijiance
{
    public TypeOfShuxing type;
    public bool dayu;
    public int shuzhi;

}
public class ShiJianManager : MonoBehaviour
{
    public TextAsset text;
    public List<ShiJian> ZhongShiJians=new List<ShiJian>();
    public List<ShiJian> shijianIN = new List<ShiJian>();
    public Dictionary<string, List<ShiJian>> shuxing=new Dictionary<string, List<ShiJian>>();
    public static ShiJianManager instance;
    public List<string> dangQian = new List<string>();
    public List<string> dangQianShuXing=new List<string>();
    
    private void Awake()
    {
        instance = this;
    }
    private void Start()
    {
        LoadShijian();
        shujukuinit();
        danqianCX();

        shiJianINInit();


        
    }
    public void LoadShijian()
    {

        string[] dataRow = text.text.Split('\n');
        
        foreach (var row in dataRow)
        {
            Debug.Log(row);
            ShiJian shiJian=new ShiJian();
            string[] rowArray = row.Split(',');
            if(rowArray[0]=="$"|| rowArray[0] =="")
            {
                continue;
            }
            shiJian.id = int.Parse(rowArray[0]);
            shiJian.neirong = rowArray[1];
            shiJian.xiyou = int.Parse(rowArray[2]);
            shiJian.shuzhijiances = new List<shuzhijiance>();
            if (rowArray[3]!="")
            {
                string[] rowShizhijianche = rowArray[3].Split('$');
                for(int i=0;i<rowShizhijianche.Length;i++)
                {
                    shuzhijiance shu = new shuzhijiance();
                    int k = 0;
                    shu.dayu = (k = rowShizhijianche[i].IndexOf('>')) < 0;
                    if(shu.dayu)
                    {
                        k = rowShizhijianche[i].IndexOf('<');
                    }
                    TypeOfShuxing ty = Enum.Parse<TypeOfShuxing>(rowShizhijianche[i].Substring(0, k));
                    
                    int shuzhi = int.Parse(rowShizhijianche[i].Substring(k + 1, rowShizhijianche[i].Length-(k+1)));
                    shu.type = ty;
                    shu.shuzhi = shuzhi;
                    shiJian.shuzhijiances.Add(shu);
                }
            }
            if (rowArray[4] != "")
                shiJian.ewaineirong = rowArray[4];

            shiJian.jiacheng = new List<int>();
            for (int i=0;i<7;i++)
            {
                int k = 0;
                if(rowArray[5+i]!="")
                {
                    k = int.Parse(rowArray[5 + i]);
                }
                Debug.Log(k);
                shiJian.jiacheng.Add(k);
            }
           
            
                shiJian.shuxin = rowArray[12];
            
            shiJian.lastIn = new List<int>();
            shiJian.nextIn = new List<int>();
            if (rowArray[13] != "")
            {
                string[] lastss= rowArray[13].Split('$');
                for (int i = 0; i < lastss.Length; i++)
                {
                    int k;
                    k = int.Parse(lastss[i]);
                    shiJian.lastIn.Add(k);
                    shiJian.last += 1;
                }
            }
            shiJian.lastEx = new List<int>();
            shiJian.nextEx = new List<int>();
            if (rowArray[14] != "")
            {
                string[] lastss = rowArray[13].Split('$');
                for (int i = 0; i < lastss.Length; i++)
                {
                    int k;
                    k = int.Parse(lastss[i]);
                    shiJian.lastEx.Add(k);
                    
                }
            }
            shiJian.cycle = rowArray[15] == "1";

            ZhongShiJians.Add(shiJian);
        }
    }


    public void shujukuinit()
    {
        for(int i=0;i<ZhongShiJians.Count;i++)
        {
           
            if (ZhongShiJians[i].lastEx.Count != 0)
            {
                for (int k = 0; k < ZhongShiJians[i].lastEx.Count; k++)
                {
                    ZhongShiJians[ZhongShiJians[i].lastIn[k] - 1].nextEx.Add(i + 1);

                }
            }
             if ( ZhongShiJians[i].lastIn.Count!=0)
            {
                for(int k=0;k< ZhongShiJians[i].lastIn.Count;k++)
                {
                    ZhongShiJians[ZhongShiJians[i].lastIn[k] - 1].nextIn.Add(i+1);

                }
                continue;
            }
            if(shuxing.ContainsKey(ZhongShiJians[i].shuxin))
            {
                shuxing[ZhongShiJians[i].shuxin].Add(ZhongShiJians[i]);
                Debug.Log(ZhongShiJians[i].shuxin);
            }
            else
            {
                shuxing.Add(ZhongShiJians[i].shuxin, new List<ShiJian>());
                shuxing[ZhongShiJians[i].shuxin].Add(ZhongShiJians[i]);
            }
            
        }

    }

    public void shiJianINInit()
    {
        for(int i=0;i<dangQianShuXing.Count;i++)
        {
            if(shuxing.ContainsKey(dangQianShuXing[i]))
            {
                shijianIN.AddRange(shuxing[dangQianShuXing[i]]);
            }

        }
    }

    public void danqianCX()
    {
        List<string> s = new List<string>();
        for (int i = 1; i <= dangQian.Count; i++)
        {
            dangqianInit(s, 0, i);
        }
       
        
    }
    public void dangqianInit(List<string> str,int i,int L)
    {
        if(L==0)
        {
            dangQianShuXing.Add(string.Join("$", str));
            return;
        }
        if (i == dangQian.Count)
            return;
        str.Add(dangQian[i]);
        
        dangqianInit(str, i + 1, L - 1);
        str.RemoveAt(str.Count - 1);
        
        dangqianInit(str, i + 1, L);



    }
    public ShiJian ramShijian()
    {
        int quanzhi = 0;
        for(int i=0;i<shijianIN.Count;i++)
        {
            quanzhi += (int)Math.Pow(2, shijianIN[i].xiyou - 1);
        }
        int ram = UnityEngine.Random.Range(0, quanzhi);
        Debug.Log(ram);
        for (int i = 0; i < shijianIN.Count; i++)
        {
            ram -= (int)Math.Pow(2, shijianIN[i].xiyou - 1);
            if(ram<0)
            {
                return shijianIN[i];
            }
        }
        return null;
    }
}
